/****** Object:  StoredProcedure [dbo].[web_renew_groups]    Script Date: 10/19/2013 09:22:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'web_ins_upd_role')
DROP PROCEDURE web_ins_upd_role
GO

-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[web_ins_upd_role] 
@uid varchar(100),
@pav varchar(100)
AS
BEGIN



SET NOCOUNT ON

BEGIN TRANSACTION

If @uid='0'
begin
	insert into ws_roles_h(role_pav) values (@pav)
	set @uid=SCOPE_IDENTITY() 
end	
else 
    update ws_roles_h set role_pav=@pav where role_id=@uid
	
delete from ws_roles_b where role_id=@uid

DECLARE @tmp varchar(100)


DECLARE MY_CURSOR CURSOR 
  LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR 
SELECT kodas
FROM #tt_eilutes

OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @tmp
WHILE @@FETCH_STATUS = 0
BEGIN 

insert into ws_roles_b (method_pav,role_id ) 
values (@tmp,@uid)

FETCH NEXT FROM MY_CURSOR INTO @tmp
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR


COMMIT TRANSACTION

END

GO



